package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/26 14:34
 */
@RestController
@RequestMapping(value = "/user/user")
@Api(tags = "C端用户相关接口")
@Slf4j
public class UserController {
    @Resource
    private JwtProperties jwtProperties;
    @Resource
    private UserService userService;

    @PostMapping(value = "/login")
    @ApiOperation(value = "用户登录")
    public Result<UserLoginVO> userLogin(@RequestBody UserLoginDTO userLoginDTO) {
        // 登录
        User user = userService.login(userLoginDTO);
        // 增加jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        // 创建返回对象
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token).build();
        return Result.success(userLoginVO);
    }

    /**
     * 用户退出
     * @return
     */
    @PostMapping(value = "/logout")
    @ApiOperation(value = "用户退出")
    public Result logout(){
        return Result.success();
    }
}
